// pages/profile/profile.js
import { checkLoginAndRedirect } from '../../utils/auth.js'

const app = getApp()

Page({
  data: {
    userInfo: null,
    isLoggedIn: false,
    menuItems: [
      {
        icon: '/images/icons/order.png',
        title: '我的订单',
        path: '/pages/order-list/order-list'
      },
      {
        icon: '/images/icons/address.png',
        title: '收货地址',
        path: '/pages/address/address'
      },
      {
        icon: '/images/icons/coupon.png',
        title: '优惠券',
        path: '/pages/coupon/coupon'
      },
      {
        icon: '/images/icons/favorite.png',
        title: '我的收藏',
        path: '/pages/favorite/favorite'
      },
      {
        icon: '/images/icons/feedback.png',
        title: '意见反馈',
        path: '/pages/feedback/feedback'
      },
      {
        icon: '/images/icons/about.png',
        title: '关于我们',
        path: '/pages/about/about'
      }
    ]
  },

  onLoad() {
    this.checkLoginStatus()
  },

  onShow() {
    // 每次显示时检查登录状态
    this.checkLoginStatus()
  },

  // 检查登录状态
  checkLoginStatus() {
    const userInfo = app.globalData.userInfo
    const isLoggedIn = !!userInfo
    
    this.setData({
      userInfo,
      isLoggedIn
    })
  },

  // 跳转到登录页
  goToLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除用户信息
          app.clearUserInfo()
          
          // 更新页面状态
          this.setData({
            userInfo: null,
            isLoggedIn: false
          })
          
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          })
        }
      }
    })
  },

  // 跳转到菜单项
  goToMenuItem(e) {
    const { path } = e.currentTarget.dataset
    
    if (!this.data.isLoggedIn) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        confirmText: '去登录',
        success: (res) => {
          if (res.confirm) {
            this.goToLogin()
          }
        }
      })
      return
    }
    
    wx.navigateTo({
      url: path
    })
  },

  // 跳转到订单列表
  goToOrders() {
    if (!checkLoginAndRedirect()) return
    
    wx.switchTab({
      url: '/pages/order-list/order-list'
    })
  },

  // 跳转到地址管理
  goToAddress() {
    if (!checkLoginAndRedirect()) return
    
    wx.navigateTo({
      url: '/pages/address/address'
    })
  },

  // 联系客服
  contactService() {
    wx.showModal({
      title: '联系客服',
      content: '客服电话：400-123-4567\n工作时间：9:00-21:00',
      confirmText: '拨打',
      success: (res) => {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '400-123-4567'
          })
        }
      }
    })
  },

  // 分享功能
  onShareAppMessage() {
    return {
      title: '美味外卖，尽在指尖',
      path: '/pages/index/index',
      imageUrl: '/images/share.jpg'
    }
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '美味外卖，尽在指尖',
      imageUrl: '/images/share.jpg'
    }
  }
})